package com.tduck.cloud.system.entity;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tduck.cloud.common.entity.SysBaseEntity;
import com.tduck.cloud.common.jackson.LongToStringSerializer;
import com.tduck.cloud.excel.annotation.ExcelDictFormat;
import com.tduck.cloud.excel.annotation.ExcelPropertyType;
import com.tduck.cloud.excel.convert.ExcelDictConvert;
import com.tduck.cloud.excel.convert.ExcelLongListConvert;
import lombok.Data;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户对象 sys_user
 *
 * @author smalljop
 */
@Data
@ExcelIgnoreUnannotated
@TableName("sys_user")
public class SysUserEntity extends SysBaseEntity {

    /**
     * 用户ID
     */
    @ExcelProperty(value = "用户序号")
    @JsonSerialize(using = LongToStringSerializer.class)
    private Long id;

    /**
     * 部门ID
     */
    @ExcelProperty(value = "部门编号")
    @ExcelPropertyType(type = ExcelPropertyType.TypeEnum.IMPORT)
    private Long deptId;

    /**
     * 用户账号
     */
    @ExcelProperty(value = "登录名称")
    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
    private String userName;

    /**
     * 用户昵称
     */
    @ExcelProperty(value = "用户名称")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    private String nickName;

    /**
     * 用户邮箱
     */
    @ExcelProperty(value = "用户邮箱")
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    private String email;

    /**
     * 手机号码
     */
    @ExcelProperty(value = "手机号码")
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    private String phonenumber;

    /**
     * 用户性别
     */
    @ExcelProperty(value = "用户性别", converter = ExcelDictConvert.class)
    @ExcelDictFormat(readConverterExp = "0=男,1=女,2=未知")
    private String sex;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 密码
     */
    @ExcelProperty(value = "密码")
    @ExcelPropertyType(type = ExcelPropertyType.TypeEnum.IMPORT)
    private String password;

    /**
     * 盐加密
     */
    @TableField(exist = false)
    private String salt;

    /**
     * 帐号状态（0正常 1停用）
     */
    @ExcelProperty(value = "帐号状态", converter = ExcelDictConvert.class)
    @ExcelDictFormat(readConverterExp = "0=正常,1=停用")
    private String status;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    private String delFlag;

    /**
     * 最后登录IP
     */
    @ExcelProperty(value = "最后登录IP")
    private String loginIp;

    /**
     * 最后登录时间
     */
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private LocalDateTime loginDate;


    /**
     * 部门对象
     */
    @TableField(exist = false)
    private SysDeptEntity dept;

    /**
     * 角色对象
     */
    @TableField(exist = false)
    private List<SysRoleEntity> roles;

    /**
     * 角色组
     */
    @TableField(exist = false)
    @ExcelProperty(value = "角色Id", converter = ExcelLongListConvert.class)
    @ExcelPropertyType(type = ExcelPropertyType.TypeEnum.IMPORT)
    private List<Long> roleIds;

    /**
     * 岗位组
     */
    @TableField(exist = false)
    @ExcelProperty(value = "部门岗位Id", converter = ExcelLongListConvert.class)
    @ExcelPropertyType(type = ExcelPropertyType.TypeEnum.IMPORT)
    private List<Long> postIds;
    /**
     * 备注
     */
    @TableField(exist = false)
    private String remark;

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }

    @JsonIgnore
    public boolean isAdmin() {
        return isAdmin(this.id);
    }

    @JsonIgnore
    @JsonProperty
    public String getPassword() {
        return password;
    }
}
